package com.functest.orm

import com.functest.framework.BaseFunctionalTest
import org.testng.annotations.Test

/**
 * Created with IntelliJ IDEA.
 * User: KovalevAS
 * Date: 05.09.13
 * Time: 17:51
 * To change this template use File | Settings | File Templates.
 */
class FieldTest extends BaseFunctionalTest {
    @Test
    void hashCodeTest() {
        asTest {
            Field f1 = new Field([name: 'name', from: ['name'] ])
            Field f2 = new Field([name: 'name', from: ['name'] ])
            asStep('checking hashcode for equal objects') {
                com.functest.framework.Assertions.weakAssertSilently('hashcode doesn\'t matches for equal objects', f1.hashCode() == f2.hashCode())
            }


            asStep('checking hashcode for different objects') {
                f2.name = 'id'
                com.functest.framework.Assertions.weakAssertSilently('error: hashcode matches for different objects', f1.hashCode() != f2.hashCode())
            }
        }
    }

    @Test
    void equalsTest() {
        asTest {
            Field x = new Field([ name: 'name', from: ['name'] ])
            Field y = new Field([ name: 'name', from: ['name'] ])
            Field z = new Field([ name: 'name', from: ['name'] ])
            assert x.equals(y)
            assert y.equals(z)
            Field notX = new Field([name: 'id', from: ['name']])
            asStep("reflexive test") {
                com.functest.framework.Assertions.weakAssertSilently("reflexive test failed", x.equals(x))
            }
            asStep("symmetric test") {
                com.functest.framework.Assertions.weakAssertSilently("symmetric test failed",
                        (x.equals(y) == y.equals(x))
                                && (x.equals(notX) == notX.equals(x)))
            }
            asStep("transitive test") {
                com.functest.framework.Assertions.weakAssertSilently("transitive test failed",
                        x.equals(y) == y.equals(z) == x.equals(z))
            }
            asStep("consistent test") {
                1.upto(10) {
                    com.functest.framework.Assertions.weakAssertSilently("consistent test failed", x.equals(y) && !x.equals(notX))
                }
            }
            asStep("null test") {
                com.functest.framework.Assertions.weakAssertSilently("null test failed", !x.equals(null))
            }
        }
    }

    @Test
    void isSimpleNameTest() {
        asTest {
            asStep('positive test') {
                com.functest.framework.Assertions.weakAssertSilently('simple name must return true', Field.isSimpleName('simpleName'))
            }
            asStep('negative test') {
                com.functest.framework.Assertions.weakAssertSilently('complicated name must return false', !Field.isSimpleName('complicated.name'))
            }
            asStep('null test') {
                com.functest.framework.Assertions.weakAssertSilently('null is a simple name', Field.isSimpleName(null))
            }
            asStep('empty string test') {
                com.functest.framework.Assertions.weakAssertSilently('empty string must return true', Field.isSimpleName(''))
            }
        }
    }

}
